package com.iwhalecloud.bss.kite.cucc.service.scene.query;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.api.scene.query.IQueryScenePromotionOfferInstService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.dto.inst.OfferInstDTO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotBlank;
import com.iwhalecloud.bss.kite.dataservice.bo.impl.QueryPromotionOfferInstBO;
import com.iwhalecloud.bss.kite.manager.api.IOperateInstTemplateService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author G
 * @since 2019/6/14
 */
@Service("queryScenePromotionOfferInstService")
public class QueryScenePromotionOfferInstService extends QuerySceneOfferInstService implements IQueryScenePromotionOfferInstService {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateInstTemplateService operateInstTemplateService;

    @Autowired
    private QueryPromotionOfferInstBO queryPromotionOfferInstBO;

    /**
     * 通过客户标识查询在用的促销销售品实例信息列表
     *
     * @param offerInstId
     * @param custId
     * @return
     */
    @Override
    @Validated
    public DataResult<List<OfferInstDTO>> listPromotionOfferInstsFromMainOfferInst(@KiteNotBlank String sceneInstId,
                                                                                   @KiteNotBlank String offerInstId, @KiteNotBlank String custId) {
        List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listPromotionOfferInsts(sceneInstId);
        List<OfferInstDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteOfferInstList)) {
            dtoList = CopyToDtoUtil.transOfferInstDtoList(kiteOfferInstList);
            // TODO 待确认 装箱
        }
        // else {
        // dtoList = queryPromotionOfferInstBO.listPromotionOfferInstByMainOfferInst(offerInstId, custId);
        // checkAndPutOfferInstList(dtoList, queryPromotionOfferInstBO);
        // }
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    /***
     * 通过查询号码加入的促销；
     *
     * @param accNum
     * @param custId
     * @return
     */
    @Override
    @Validated
    public DataResult<List<OfferInstDTO>> listPromotionOfferInstByAccNum(@KiteNotBlank String accNum,
                                                                         @KiteNotBlank String custId) {
        List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listPromotionOfferInstsByAccNum(custId, accNum);
        List<OfferInstDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteOfferInstList)) {
            dtoList = CopyToDtoUtil.transOfferInstDtoList(kiteOfferInstList);
            // TODO 待确认 装箱
        }
        // else {
        // dtoList = queryPromotionOfferInstBO.listPromotionOfferInstByAccNum(accNum, custId);
        // checkAndPutOfferInstList(dtoList, queryPromotionOfferInstBO);
        // }
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    /***
     * 查询产品实例ID加入的促销；
     *
     * @param accessProdInstId
     * @param custId
     * @return
     */
    @Override
    @Validated
    public DataResult<List<OfferInstDTO>> listPromotionOfferInstByProdInstId(@KiteNotBlank String sceneInstId,
                                                                             @KiteNotBlank String accessProdInstId, @KiteNotBlank String custId) {
        List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listPromotionOfferInsts(sceneInstId);
        List<OfferInstDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteOfferInstList)) {
            dtoList = CopyToDtoUtil.transOfferInstDtoList(kiteOfferInstList);
            // TODO 待确认 装箱
        }
        // else {
        // dtoList = queryPromotionOfferInstBO.listPromotionOfferInstByProdInstId(accessProdInstId, custId);
        // checkAndPutOfferInstList(dtoList, queryPromotionOfferInstBO);
        // }
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    /***
     * 通过促销销售品实例Id获取属性集合
     * @param sceneInstId
     * @param promotionOfferInstId
     * @param custId
     * @return
     */
    @Override
    public DataResult<Map<String, List<InstAttrDTO>>> listMainOfferInstAttrsByPromotionOfferInstId(String sceneInstId, String promotionOfferInstId, String custId) {
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        // 获取促销下面对应的主套餐的实例对象；
        List<KiteOfferInst> kiteMainOfferInsts = querySceneInstService.listMainOfferInstsByPromotionOfferInstId(custId,
            promotionOfferInstId);
        KiteOfferInst kiteOfferInst = querySceneInstService.getOfferInst(custId, promotionOfferInstId);
        if (!KiteListUtils.isEmpty(kiteMainOfferInsts)) {
            kiteMainOfferInsts.add(kiteOfferInst);
        }
        else {
            kiteMainOfferInsts = Lists.newArrayList(kiteOfferInst);
        }
        return ErrorConsts.SUCCESS.getResult(CopyToDtoUtil.transMapDataResult(sceneInst, kiteMainOfferInsts));
    }
}
